package com.thinksns.sociax.t4.android.topic;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.thinksns.sociax.android.R;
import com.thinksns.sociax.t4.model.ModelTopic;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/10/18.
 */

public class TopicAdapter extends BaseAdapter {
    Context context;
    List<ModelTopic> topics;

    public TopicAdapter(Context context, List<ModelTopic> topics) {
        if (topics == null) {
            topics = new ArrayList<>();
        }
        this.context = context;
        this.topics = topics;
    }

    @Override
    public int getCount() {
        if (topics == null) {
            return 0;
        }
        return topics.size();
    }

    public void refresh(List<ModelTopic> topics) {
        this.topics.clear();
        if (topics != null) {
            this.topics.addAll(topics);
        }
        notifyDataSetChanged();

    }

    @Override
    public Object getItem(int position) {
        return topics.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_topic, parent, false);
            holder.tv_Name = (TextView) convertView.findViewById(R.id.tv_topic_name);
            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.tv_Name.setText(topics.get(position).getTopic_name());

        return convertView;
    }

    class ViewHolder {
        TextView tv_Name;
    }
}
